//
//  HomeViewController.m
//  AngelaBaby
//
//  Created by lanou3g on 15/10/13.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "HomeViewController.h"
#import "dataHelper.h"
#import "ShowImageCell.h"
#import "CircleLayout.h"
#import "DownloadViewController.h"
#import "HomeCollectionViewCell.h"
#import "HomeModel.h"
#import "UIImageView+WebCache.h"

#define  KURL @"http://fdfs.xmcdn.com/group13/M07/39/03/wKgDXVWZ0HbR14UvAFofMr4WrlU953.mp3"
#define KCellIdentifier @"identifier"
#define KHOMEURL @"http://app.9nali.com/805/common_tag/6/all"
#define KKKurl @"http://app.9nali.com/805/common_tag/6/%E5%84%BF%E6%AD%8C%E5%A4%A7%E5%85%A8"


@interface HomeViewController ()

@property(nonatomic,strong)dataHelper *datahandle;

@property(nonatomic,strong)DownloadViewController *download;

@property (nonatomic,strong) UICollectionView *collectionV;

@property(nonatomic,strong) NSArray *titleArray;

@property(nonatomic,strong) NSArray *albumArray;

@property(nonatomic,strong)UIScrollView *Scroll;

@property(nonatomic,strong)HomeCollectionViewCell *value;

@end

@implementation HomeViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   self.view.backgroundColor=[UIColor whiteColor];
    _datahandle =[dataHelper shareDataHelper];
    
    _value=[HomeCollectionViewCell new];
    _value.delegate=self;
    
    [self setCollectionView];
    [self setCollectionV];
}


-(void)translateID:(int)sender{
    NSLog(@"hhh%d",sender);
    
}





-(void)setCollectionV{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection=UICollectionViewScrollDirectionVertical;
    layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 20);
    layout.itemSize=CGSizeMake(self.view.frame.size.width, 150);
    layout.minimumInteritemSpacing=10;
    layout.minimumInteritemSpacing=20;

    self.collectionV=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    _collectionV.delegate=self;
    _collectionV.dataSource=self;
    _collectionV.backgroundColor=[UIColor lightGrayColor];
    [_collectionV registerNib:[UINib nibWithNibName:@"HomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"collectionV"];
    
    _albumArray=[NSArray array];
    _titleArray=@[@"今日推荐",@"晚安宝贝",@"精品专辑",@"最新首发",@"宝贝最爱"];
    HomeModel *mm=[[HomeModel alloc]init];
    [_datahandle getDataWithUrl:KHOMEURL dic:@{@"page_id":@41} model:mm returnArray:^(NSMutableArray *arra) {
        _albumArray=arra;
        [self.collectionV reloadData];
    }];
    
    [self.collectionV addSubview:self.collectionView];
    [self.view addSubview:_collectionV];
    
}

//轮播图
-(void)setCollectionView{

    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height/4.f;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, height) collectionViewLayout:[[CircleLayout alloc] init]];
    
    [self.collectionView registerClass:[ShowImageCell class] forCellWithReuseIdentifier:KCellIdentifier];
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    self.collectionView.delegate = self;
    [self.collectionView setContentOffset:CGPointMake(width, 0.0F)];
    self.collectionView.dataSource = self;
    
//    _download =[DownloadViewController downloadViewController];
//    [_download testDownLoad:KURL];
//    [self presentViewController:_download animated:YES completion:nil];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionViewDelegate
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView==self.collectionView) {
        return 1;
    }else if(collectionView==self.collectionV){
        return _albumArray.count/4;
    }
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView==self.collectionView) {
         return 10;
    }else if(collectionView==self.collectionV){
        return 1;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (cView == self.collectionView) {
        ShowImageCell *cell = (ShowImageCell *)[cView dequeueReusableCellWithReuseIdentifier:KCellIdentifier
        forIndexPath:indexPath];
        if (!cell) {
            return nil;
        }
        NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",(long)indexPath.row];
        
        cell.imageView.image = [UIImage imageNamed:imageName];
        UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        [cell.imageView addGestureRecognizer:tapGesture];
        return cell;
    }else if (cView==self.collectionV){
        
        HomeCollectionViewCell *cell=[cView dequeueReusableCellWithReuseIdentifier:@"collectionV" forIndexPath:indexPath];
        cell.title.text=_titleArray[indexPath.section];
        
        HomeModel *model=[[HomeModel alloc]init];
        model=_albumArray[indexPath.section*3];
        [cell.firstImage sd_setImageWithURL:[NSURL URLWithString:model.coverSmall]];
        cell.firstBtn.font=[UIFont systemFontOfSize:14];
        cell.firstBtn.text=model.title;
        [cell.firstID setTitle:[NSString stringWithFormat:@"%d",model.uid] forState:UIControlStateNormal];
        cell.firstID.tintColor=[UIColor clearColor];

        
        HomeModel *model1=[[HomeModel alloc]init];
        model1=_albumArray[indexPath.section*3+1];
        [cell.secondImage sd_setImageWithURL:[NSURL URLWithString:model1.coverSmall]];
         cell.secondBtn.font=[UIFont systemFontOfSize:14];
        cell.secondBtn.text=model1.title;
        [cell.secondID setTitle:[NSString stringWithFormat:@"%d",model.uid] forState:UIControlStateNormal];
        cell.secondID.tintColor=[UIColor clearColor];

        
        HomeModel *model2=[[HomeModel alloc]init];
        model2=_albumArray[indexPath.section*3+2];
        [cell.thirdImage sd_setImageWithURL:[NSURL URLWithString:model2.coverSmall]];
         cell.thirdBtn.font=[UIFont systemFontOfSize:14];
        cell.thirdBtn.text=model2.title;
        [cell.thirdID setTitle:[NSString stringWithFormat:@"%d",model.uid] forState:UIControlStateNormal];
        cell.thirdID.tintColor=[UIColor clearColor];
        
        return cell;
    }
  
    return nil;
}




-(void)tapGesture:(UITapGestureRecognizer *)sender{
    
}




#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (_scrollView==self.collectionView) {
        float targetX = _scrollView.contentOffset.x;
        NSInteger numCount = [self.collectionView numberOfItemsInSection:0];
        float ITEM_WIDTH = _scrollView.frame.size.width;
        
        if (numCount>=3)
        {
            if (targetX < ITEM_WIDTH/2) {
                [_scrollView setContentOffset:CGPointMake(targetX+ITEM_WIDTH *numCount, 0)];
            }
            else if (targetX >ITEM_WIDTH/2+ITEM_WIDTH *numCount)
            {
                [_scrollView setContentOffset:CGPointMake(targetX-ITEM_WIDTH *numCount, 0)];
            }
        }
        

    }
  }
 
 
 

 
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
